package com.codeoss.framework.view.parser;

import org.springframework.stereotype.Service;

import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.ExpressionUtil;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.ShortcutMenu;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Service("shortcutmenuParser")
public class ShortcutMenuParser extends AbstractComponentParser {
	@Override
	protected void setAttributes(IParseContext parseContext, ObjectNode objectNode, Component component) {
		super.setAttributes(parseContext, objectNode, component);
		ShortcutMenu shortcutMenu=(ShortcutMenu)component;
		String title = objectNode.get("title").asText();
		shortcutMenu.setTitle(title);
		
		int col = objectNode.get("col").asInt();
		shortcutMenu.setCol(col);
		
		ObjectNode propertyMapNode=(ObjectNode)objectNode.get("propertyMap");
		if(propertyMapNode!=null) {
			JsonNode jsonNode=propertyMapNode.get("cols");
			if(jsonNode!=null) {
				shortcutMenu.setCols(jsonNode.asInt());
			}
			jsonNode=propertyMapNode.get("dataSourceScript");
			if(jsonNode!=null) {
				String dataSourceScript=jsonNode.asText();
				if(!StringUtils.isEmpty(dataSourceScript)) {
					Expression expression=ExpressionUtil.convertToExpression(dataSourceScript, Expression.EXPRESSION_TYPE_JAVASCRIPT);
					shortcutMenu.setDataSourceScript(expression);
				}
			}
		}
	}
	
	@Override
	protected Component createComponent(IParseContext parseContext, ObjectNode objectNode) {
		// TODO Auto-generated method stub
		return new ShortcutMenu();
	}

}
